package com.epro.dx.adapter;

import java.util.List;

import android.content.ActivityNotFoundException;
import com.epro.dx.R;
import com.epro.dx.activity.LPLocalBrowserActivity;
import com.epro.dx.activity.SpecialActivity;
import com.epro.dx.indicator.IconPagerAdapter;
import com.epro.dx.util.ImageLoaderUtils;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.weipu.common.facade.model.BannerModel;
import com.weipu.common.facade.model.ModuleChildrenModel;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import com.weipu.common.util.Logger;

public class HomeBannerAdapter extends PagerAdapter implements IconPagerAdapter {
	private final static String TAG = "HomeBannerAdapter";
	private Context mContext;

	private List<ModuleChildrenModel> bannerModels;

	private int[] icons;
	/**
	 * @param mContext
	 *            上下文
	 * @param imgIds
	 *            要展示的图片id数组
	 */

	private DisplayImageOptions options;

	private onLinkBannerClickListenner onclickListenner;

	private Tracker traker;

	public HomeBannerAdapter(Context mContext,
			List<ModuleChildrenModel> bannerModels, Tracker traker) {
		this.mContext = mContext;
		this.bannerModels = bannerModels;
		this.traker = traker;
		icons = new int[bannerModels.size()];
		setIconValue();
		options = ImageLoaderUtils.getImageLoaderOptions(
				R.drawable.default488_488, R.drawable.default488_488);
	}

	public void setOnLinkBannerClickListenner(
			onLinkBannerClickListenner onclickListenner) {
		this.onclickListenner = onclickListenner;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		icons = new int[bannerModels.size()];
		setIconValue();
	}

	private void setIconValue() {
		for (int i = 0; i < bannerModels.size(); i++) {
			icons[i] = R.drawable.index_icon_selector_first_page;
		}
	}

	@Override
	public int getCount() {
		if (bannerModels != null)
			return bannerModels.size();
		return 0;
	}

	public void setBannerModels(List<ModuleChildrenModel> bannerModels) {
		this.bannerModels = bannerModels;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		ImageView imageView = new ImageView(mContext);
		imageView.setScaleType(ScaleType.FIT_XY);
		final ModuleChildrenModel bModel = bannerModels.get(position);
		String filePath = bModel.getPhoneImg();
		if (!TextUtils.isEmpty(filePath) && null != imageView) {
			imageView.setTag(filePath);
			// imageWorker.loadImage(filePath, imageView);
			ImageLoader.getInstance()
					.displayImage(filePath, imageView, options);
		} else {
			imageView.setImageResource(R.drawable.default488_488);

		}
		imageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (null != bModel) {
					traker.setScreenName("Home");
					traker.send(new HitBuilders.EventBuilder()
							.setCategory("Click Banner")
							.setAction("BannerName: " + bModel.getName())
							.build());
					if (null != onclickListenner) {
						onclickListenner.onClickBanner(bModel);
						return;
					}

				}
			}
		});

		container.addView(imageView);
		return imageView;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public int getItemPosition(Object object) {
		// return super.getItemPosition(object);
		return POSITION_NONE;
	}

	@Override
	public int getIconResId(int index) {
		return icons[index % icons.length];
	}

	@Override
	public int getIconCount() {
		if (bannerModels != null)
			return bannerModels.size();
		return 0;
	}

	public interface onLinkBannerClickListenner {
		void onClickBanner(ModuleChildrenModel mcModel);
	}

}
